package com.xd.common.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;

import java.util.Map;

public class AlipayUtil {


    //统一收单下单并支付页面接口
    public static void pay() throws JSONException, AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipay.com/gateway.do",
                "2021004103681364",
                "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDaDkPHnTlN/2L5a0f0YcWA6e+rQVeFUsaSa5RPuIixbOz5BzM5keh14ORmZbm+9X3ge4IteUQLgrW2KnHZz3xdv63WniFW1BzHro4s3YdvUaXmUIwMjmeRbPThaYsVl+rujSI4+wyqdbRs6jkYYhMy2aEHhX5av+zKu2BPjKua7YGwEOMfvGDpUBV7PHsVFxOLe7AhSuv7pYIOnKDborDefUhBs+YzPD3k6qFdfbhOmgiKdwQV80Ib2w0yK4S3qJtncxJ5mfaeduHHyCy6Kx+F1c9LGUCSWZ84XYKZD4R+4mWn0DTIYnyJpcZ7qhUn9fXMrBBh0E69sR/E6EJtHdovAgMBAAECggEAY93Knx5bw2QmsTK4Fq28pcj+UkGv0sjRVvfcShKq0A+iDH+Gr2QqweoI0Pjf9Bw81VXbjfYO+vKXRGH2GJpVTB5ggH2VqU2mDLHePaALyijrwSq8qXeC6XCsSpxq7a3RFAYhWVVpPJyB0KwJECjCwDQ58tbpwqhJy6O7fVNguKgbE2Qbwv69JkbkzJ7WGVBjKG0AxhWOzSogImRUgw8s4aBR95libDqJ32ydzvmFSf6f2dp1k8nweYDi1I7grtC6X6qAsTRkvTv/WtIcGkim/wjCO8axtTSdfmmGo10Tuqf0tUhqaWEE51h2WDgnDbNJRDsibKIBWZdA5MM++Qci+QKBgQD+Dl9PrY5S2Nfr3rE2/HvVtcPq+oMPfG5fUaHoIO4AG9EwucEOXZAhMckmcTXzq0LizZPM560IrPSyU9ZfyF03UteiK+tYI4q75PIV2euFbW77hTIIVUmqcivgabBjF46+Lai7WaEub2UKp5zANzoF1pCitiAfDEK1eOXt2RjMYwKBgQDbuWCXVZpuvb7eLRBL2QbOXro5jASW2dZcwc2JEMiqgqSOQaHIW5IL4XcM4mjOtwsWJINaikgQezrS8gmIMtu33eCQUMcFbeAOqbjAAQlJU+TeFrMeuGyM33LC1dPMf7+JCO4EQOYur9dRKqBcsc3DbTqujaL7w1ZTMzbCxXzGxQKBgA55lyidBNznifXgbExrukqE3rLGryz0s/0uU4E8TxMmgpa6HqvI3OtnQMS18pL9kQQHHoKwOIWkBRUb/bzbV6RNTgfpZLFS5RarhreEAQ1LHOWrwxwCb/4MHz/cBba/otxnEZQLGUniAunGfwHco6ute/QpWE8sjah1SPRySB1tAoGAQdHWTX8nM+VDN+rn5tCsaUWXI4X+GfJHdKHZoCWW9v0LrlGa1RoZNdXkg8eO/d13RLrLXmPuEoHXrTk0Aci6aOneS1znQYkYhUvyPBITZhPri1vhbGJyogCNsC+9spTSG4rBQiTuzy19oqLSRvI8YT5nRcLIAAMT6Ph+fvoJ1sECgYAxCzm6kv6OwifmbT0ZFGoR+/EmksqIAh82ONP2sjMFVJXYaTVhNQyLELHpfQmhIZHi8gietLRUzteELEj12zpewElbFJzwjCUYLaUYU5Om7fqv0CyJjRrm5BwtEpGxJQBL6tltPmzARWk7p0eKl8DNyOavKMMK0fav2gnBpgpM7g==",
                "JSON",
                "UTF-8",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnHXcBz5FEwYl0ce3iIDbLadfTBmuCc3vn+HK8yi8xdZcDWnKCiC2sK4HDpu47mUn3HGViKFfHcWDFgAUzPSJ+y7ZW+smJzf5qZWXbi5RgFLB9m35trFNBOGIrqk7JF0x/5Bz493vPTweq1+uSpBGz4qjYR0T19UWj4FU60f7eQi42AkxMiW4kd6QHhxmqmmGJ4eoUuDcy8D6tDUOjGhHVajnhLZuvUuDxmf8z1IHOlY5i3HYTkfFJ21fy84jr1sW3K7ZFySQN9cgFvotepMU4FfmVcKOSE6M/e0KU7zF7PYKMJ0ReOsMUYuVPTQ9E3/wFLNCfftyhZk9NEsdWL+oawIDAQAB",
                "RSA2",
                "OtaX+PSmUZ3d4JjZ0ENzhQ==",
                "AES");
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
//异步接收地址，仅支持http/https，公网可访问
        request.setNotifyUrl("http://sj.xy002.cn/xkd/pay/alipay/notify");
//同步跳转地址，仅支持http/https
        request.setReturnUrl("http://sj.xy002.cn/xkd/pay/alipay/return");
/******必传参数******/
        JSONObject bizContent = new JSONObject();
//商户订单号，商家自定义，保持唯一性
        bizContent.put("out_trade_no", "20210817010101004");
//支付金额，最小值0.01元
        bizContent.put("total_amount", 0.01);
//订单标题，不可使用特殊符号
        bizContent.put("subject", "测试商品");
//电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAY
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");

/******可选参数******/
//bizContent.put("time_expire", "2022-08-01 22:00:00");

//// 商品明细信息，按需传入
//JSONArray goodsDetail = new JSONArray();
//JSONObject goods1 = new JSONObject();
//goods1.put("goods_id", "goodsNo1");
//goods1.put("goods_name", "子商品1");
//goods1.put("quantity", 1);
//goods1.put("price", 0.01);
//goodsDetail.add(goods1);
//bizContent.put("goods_detail", goodsDetail);

//// 扩展信息，按需传入
//JSONObject extendParams = new JSONObject();
//extendParams.put("sys_service_provider_id", "2088511833207846");
//bizContent.put("extend_params", extendParams);
request.setNeedEncrypt(true);
        //String encrypt = AESUtil.encrypt(bizContent.toString(), "OtaX+PSmUZ3d4JjZ0ENzhQ==");
        request.setBizContent(bizContent.toString());//OtaX+PSmUZ3d4JjZ0ENzhQ==
        //String encrypt = AESUtil.encrypt(request.toString(), "OtaX+PSmUZ3d4JjZ0ENzhQ==");
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
            System.out.println(response.getBody());
        } else {
            System.out.println("调用失败");
        }
    }

    //异步验签接口
    public static void sign(Map<String, String> paramsMap) throws AlipayApiException {
        //Map<String, String> paramsMap = ...  //将异步通知中收到的所有参数都存放到 map 中
        boolean signVerified = AlipaySignature.rsaCheckV1(paramsMap, "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnHXcBz5FEwYl0ce3iIDbLadfTBmuCc3vn+HK8yi8xdZcDWnKCiC2sK4HDpu47mUn3HGViKFfHcWDFgAUzPSJ+y7ZW+smJzf5qZWXbi5RgFLB9m35trFNBOGIrqk7JF0x/5Bz493vPTweq1+uSpBGz4qjYR0T19UWj4FU60f7eQi42AkxMiW4kd6QHhxmqmmGJ4eoUuDcy8D6tDUOjGhHVajnhLZuvUuDxmf8z1IHOlY5i3HYTkfFJ21fy84jr1sW3K7ZFySQN9cgFvotepMU4FfmVcKOSE6M/e0KU7zF7PYKMJ0ReOsMUYuVPTQ9E3/wFLNCfftyhZk9NEsdWL+oawIDAQAB", "UTF-8", "RSA2");  //调用SDK验证签名
        if (signVerified) {
            // TODO 验签成功后，按照支付结果异步通知中的描述，对支付结果中的业务内容进行二次校验，校验成功后在response中返回success并继续商家自身业务处理，校验失败返回failure
        } else {
            // TODO 验签失败则记录异常日志，并在response中返回failure.
        }
    }

    //统一收单线下交易查询接口
    public static void query() throws JSONException, AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", "app_id", "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDaDkPHnTlN/2L5a0f0YcWA6e+rQVeFUsaSa5RPuIixbOz5BzM5keh14ORmZbm+9X3ge4IteUQLgrW2KnHZz3xdv63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/cBba/otxnEZQLGUniAunGfwHco6ute/QpWE8sjah1SPRySB1tAoGAQdHWTX8nM+VDN+rn5tCsaUWXI4X+GfJHdKHZoCWW9v0LrlGa1RoZNdXkg8eO/d13RLrLXmPuEoHXrTk0Aci6aOneS1znQYkYhUvyPBITZhPri1vhbGJyogCNsC+9spTSG4rBQiTuzy19oqLSRvI8YT5nRcLIAAMT6Ph+fvoJ1sECgYAxCzm6kv6OwifmbT0ZFGoR+/EmksqIAh82ONP2sjMFVJXYaTVhNQyLELHpfQmhIZHi8gietLRUzteELEj12zpewElbFJzwjCUYLaUYU5Om7fqv0CyJjRrm5BwtEpGxJQBL6tltPmzARWk7p0eKl8DNyOavKMMK0fav2gnBpgpM7g==", "json", "UTF-8", "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnHXcBz5FEwYl0ce3iIDbLadfTBmuCc3vn+HK8yi8xdZcDWnKCiC2sK4HDpu47mUn3HGViKFfHcWDFgAUzPSJ+y7ZW+smJzf5qZWXbi5RgFLB9m35trFNBOGIrqk7JF0x/5Bz493vPTweq1+uSpBGz4qjYR0T19UWj4FU60f7eQi42AkxMiW4kd6QHhxmqmmGJ4eoUuDcy8D6tDUOjGhHVajnhLZuvUuDxmf8z1IHOlY5i3HYTkfFJ21fy84jr1sW3K7ZFySQN9cgFvotepMU4FfmVcKOSE6M/e0KU7zF7PYKMJ0ReOsMUYuVPTQ9E3/wFLNCfftyhZk9NEsdWL+oawIDAQAB", "RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "20150320010101001");
//bizContent.put("trade_no", "2014112611001004680073956707");
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }


    public static void main(String[] args) {
        try {
            pay();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
    }

}
